package org.yu.api.register;


import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraftforge.common.extensions.IForgeMenuType;

import java.util.HashMap;

public  class MenuTypeBuilder<T extends AbstractContainerMenu> {
    private final LazyRegistry lazyRegistry;
    private final String name;
    private final LazyMenu.LazyMenuType<T> lazyMenuType;
    private final HashMap<String, String> langMap = new HashMap<>();
    private final MenuHolder<T> result = new MenuHolder<>();

    protected MenuTypeBuilder(LazyRegistry lazyRegistry, String name, LazyMenu.LazyMenuType<T> lazyMenuType) {
        this.lazyRegistry = lazyRegistry;
        this.name = name;
        this.lazyMenuType = lazyMenuType;
    }

    public MenuTypeBuilder<T> name(String lang, String value) {
        langMap.put(lang, value);
        return this;
    }

    private void defaultName() {
        name("en_us", LangBuilder.toTitleCase(name));
    }

    public MenuHolder<T> build() {
        result.type = lazyRegistry.menuTypes.register(name, () -> IForgeMenuType.create((windowId, inv, data) -> lazyMenuType.create(result.get(), windowId, inv, data)));
        if (!langMap.containsKey(lazyRegistry.defaultLang)) defaultName();
        langMap.forEach((lang, value) -> lazyRegistry.lang(lang, () -> new TranslationBean("menu." + lazyRegistry.modid + "." + name, value)));
        return result;
    }
}
